local skel = fk.CreateSkill {
  name = "emo__jingbo",
}

Fk:loadTranslationTable{
  ["emo__jingbo"] = "惊波",
  [":emo__jingbo"] = "你每回合首次失去牌后，可以判定并获得判定牌，然后本回合其他角色首次失去点数处于〈失去牌—判定牌〉之间的牌后，你对其造成1点冰冻伤害。",

  ["#emo__jingbo-ask"] = "惊波：你可以判定并获得判定牌",
  ["@emo__jingbo-turn"] = "惊波",
}

skel:addEffect(fk.AfterCardsMove, {
  anim_type = "drawcard",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(skel.name) and player:usedSkillTimes(skel.name) == 0 then
      local cid
      local first = player.room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
        for _, move in ipairs(e.data) do
          if move.from == player then
            for _, info in ipairs(move.moveInfo) do
              if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
                cid = info.cardId
                return true
              end
            end
          end
        end
      end, Player.HistoryTurn)[1]
      if first and first.data == data then
        event:setCostData(self, {cid = cid})
        return true
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {
      skill_name = skel.name, prompt = "#emo__jingbo-ask",
    })
  end,
  on_use = function (self, event, target, player)
    local room = player.room
    local cid = event:getCostData(self).cid
    if not cid then return end
    local judge = {
      who = player,
      reason = skel.name,
      pattern = ".",
      skipDrop = true,
    }
    room:judge(judge)
    if not judge.card then return end
    if player.dead then
      if room:getCardArea(judge.card:getEffectiveId()) == Card.Processing then
        room:moveCardTo(judge.card, Card.DiscardPile, nil, fk.ReasonJudge)
      end
    else
      local list = {Fk:getCardById(cid).number, judge.card.number}
      table.sort(list)
      room:setPlayerMark(player, "@emo__jingbo-turn", table.concat(list, "-"))
      if room:getCardArea(judge.card:getEffectiveId()) == Card.Processing then
        room:obtainCard(player, judge.card, true, fk.ReasonJustMove, player, skel.name)
      end
    end
  end,
})

skel:addEffect(fk.AfterCardsMove, {
  anim_type = "offensive",
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    if not player:hasSkill(skel.name) or player:getMark("@emo__jingbo-turn") == 0 then return end
    local nums = string.split(player:getMark("@emo__jingbo-turn"), "-")
    local num1, num2 = tonumber(nums[1]), tonumber(nums[2])
    local loseMap = {}
    for _, move in ipairs(data) do
      if move.from and move.from ~= player and not move.from.dead and not loseMap[move.from] then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerEquip or info.fromArea == Card.PlayerHand then
            loseMap[move.from] = info.cardId
            break
          end
        end
      end
    end
    if next(loseMap) == nil then return end
    local tos = {}
    for to, cid in pairs(loseMap) do
      local id
      player.room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
        for _, move in ipairs(e.data) do
          if move.from == to then
            for _, info in ipairs(move.moveInfo) do
              if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
                id = info.cardId
                return true
              end
            end
          end
        end
      end, Player.HistoryTurn)
      if id == cid and num1 <= Fk:getCardById(id).number and num2 >= Fk:getCardById(id).number then
        table.insert(tos, to)
      else
        loseMap[to] = nil
      end
    end
    if #tos > 0 then
      event:setCostData(self, {tos = tos, loseMap = loseMap })
      return true
    end
  end,
  on_trigger = function (self, event, target, player, data)
    local cost_data = event:getCostData(self)
    if not cost_data then return false end
    local tos = cost_data.tos or {}
    for _, to in ipairs(tos) do
      if not to.dead then
        self:doCost(event, to, player, data)
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    player.room:damage { from = player, to = target, damage = 1, skillName = skel.name, damageType = fk.IceDamage }
  end,
})

return skel
